import {yfuPost} from '@/utils/request.js'
import {getUserByTrueNameRemote} from '@/view/api/userManager.js'
let validateUtil = {
  install (Vue, options) {
    Vue.prototype.filter_rules = function (item) {
      let rules = []
      if (item == null) {
        return rules
      }
      if (item.required) {
        rules.push({required: true, message: '该输入项为必填项!', trigger: 'blur'})
      }
      if (item.maxLength) {
        rules.push({max: item.maxLength, message: '最多输入' + item.maxLength + '个字符!', trigger: 'blur'})
      }
      if (item.min && item.max) {
        rules.push({
          min: item.min,
          max: item.max,
          message: '字符长度在' + item.min + '至' + item.max + '之间!',
          trigger: 'blur'
        })
      }
      if (item.type) {
        let type = item.type
        switch (type) {
          case 'email':
            rules.push({type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'})
            break
          case 'qq':
            rules.push({validator: isvalidateQQ, trigger: 'blur,change'})
            break
          case 'mobile':
            rules.push({validator: isvalidateMobile, trigger: 'blur,change'})
            break
          case 'number':
            rules.push({validator: isvalidateNumber, trigger: 'blur,change'})
            break
          case 'charactor':
            rules.push({validator: isvalidateCharactor, trigger: 'blur,change'})
            break
          case 'upperCharactor':
            rules.push({validator: isvalidateUpperCharactor, trigger: 'blur,change'})
            break
          case 'letterCharactor':
            rules.push({validator: isvalidateLetterCharactor, trigger: 'blur,change'})
            break
          case 'username':
            rules.push({validator: isvalidateUserName, trigger: 'blur,change'})
            break
          case 'nameBadCharactor':
            rules.push({validator: isvalidateNameBadCharactor, trigger: 'blur,change'})
            break
          case 'trueName':
            rules.push({validator: isvalidateTrueName, trigger: 'blur,change'})
            break
          default:
            rules.push({})
            break
        }
      }
      return rules
    }
  }
}
export default validateUtil
/*
验证大写字母
 */
const isvalidateUpperCharactor = (rule, value, callback) => {
  let myreg = /^[A-Z]+$/
  if (value != null && value !== '') {
    if (!myreg.test(value)) {
      callback(new Error('格式不正确,只能为大写字母!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

/*
验证字母和数字
 */
const isvalidateCharactor = (rule, value, callback) => {
  let myreg = /^[a-zA-Z0-9]+$/
  if (value != null && value !== '') {
    if (!myreg.test(value)) {
      callback(new Error('格式不正确,只能为字母和数字!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}
/*
验证小写字母
 */
const isvalidateLetterCharactor = (rule, value, callback) => {
  let myreg = /^[a-z]+$/
  if (value != null && value !== '') {
    if (!myreg.test(value)) {
      callback(new Error('格式不正确，只能为小写字母!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

/*
验证数字
 */
const isvalidateNumber = (rule, value, callback) => {
  let myreg = /^[0-9.]*$/
  if (value != null && value !== '') {
    if (!myreg.test(value)) {
      callback(new Error('格式不正确，只能为数字!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

/*
验证QQ号码
*/
const isvalidateQQ = (rule, value, callback) => {
  let reg = /^\d{5,10}$/
  if (value != null && value !== '') {
    if (!reg.test(value)) {
      callback(new Error('您输入的QQ号不正确!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}
/*
验证手机号
 */
const isvalidateMobile = (rule, value, callback) => {
  var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/
  if (value != null && value !== '') {
    if (!myreg.test(value)) {
      callback(new Error('您输入的手机不正确!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

/*
验证用户名
 */
const isvalidateUserName = (rule, value, callback) => {
  let myreg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){3,16}$/
  if (value != null && value !== '') {
    if (!myreg.test(value)) {
      callback(new Error('您输入的用户名格式不正确!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

/*
验证名称非法字符
 */
const isvalidateNameBadCharactor = (rule, value, callback) => {
  let regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im
  let regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im
  if (value != null && value !== '') {
    if (regEn.test(value) || regCn.test(value)) {
      callback(new Error('您输入的名称包含特殊符号!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}
/*
验证真实姓名是否存在
 */
const isvalidateTrueName = (rule, value, callback) => {
  let regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im
  let regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im
  if (value != null && value !== '') {
    if (regEn.test(value) || regCn.test(value)) {
      callback(new Error('您输入的名称包含特殊符号!'))
    } else {
      let param = {}
      param.trueName = value
      yfuPost(getUserByTrueNameRemote.url, param, (data) => {
        if (data === '姓名可以注册') {
          callback()
        } else {
          callback(new Error('用户名已存在'))
        }
      })
    }
  } else {
    callback()
  }
}
